﻿namespace BM.Tools.WebControls.MForm.Design
{
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Web.UI;
    using System.Web.UI.Design;

    using BM.Tools.WebControls.MForm.Controls;
    using BM.Tools.WebControls.MForm.Generator;

    /// <summary>
    /// This is the <see cref="ControlDesigner" /> class of the <see cref="Root" /> control.
    /// </summary>
    internal class RootDesigner : OffspringsOwnerDesigner
    {        
        private DesignerActionListCollection actionLists;
        
        private FormBuilderMode designerMode = FormBuilderMode.View;

        /// <summary>
        /// Gets ActionLists.
        /// </summary>
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (null == actionLists)
                {
                    actionLists = new DesignerActionListCollection();
                    actionLists.Add(new RootActionList(this, ViewControl as Root));
                }

                return actionLists;
            }
        }

        /// <summary>
        /// Gets or sets the designer mode.
        /// </summary>
        public FormBuilderMode DesignerMode
        {
            get
            {
                return designerMode;
            }

            set
            {
                designerMode = value;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the control is editable.
        /// </summary>
        public override bool Editable
        {
            get
            {
                return DesignerMode == FormBuilderMode.Edit;
            }
        }

        /// <summary>
        /// Clears the template with a specified name
        /// </summary>
        /// <param name="component">
        /// The component.
        /// </param>
        /// <param name="templateName">
        /// The template name.
        /// </param>
        internal void ResetTemplate(IComponent component, string templateName)
        {
            InvokeTransactedChange(Component, ResetTemplateCallBack, new Pair(component, templateName), string.Empty);
            UpdateDesignTimeHtml();
        }

        /// <summary>
        /// Retruns the HTML design time code when the Root control is empty
        /// </summary>
        /// <returns>
        /// a HTML design time code
        /// </returns>
        protected override string GetEmptyDesignTimeHtml()
        {
            return
                    CreatePlaceHolderDesignTimeHtml(
                            "Switch to source view to edit the content or generate from the schema file");
        }

        private static bool ResetTemplateCallBack(object context)
        {
            Pair pair = (Pair)context;
            IComponent first = (IComponent)pair.First;
            string second = (string)pair.Second;
            TypeDescriptor.GetProperties(first)[second].ResetValue(first);
            return true;
        }
    }
}